package com.fishbone.cloud.auth.security.config;

import com.fishbone.cloud.auth.security.support.FishBoneJwtTokenEnhancer;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import java.security.KeyPair;

/**
 * @ClassName FishBoneJwtTokenStoreConfiguration
 * @Author huyaxi
 * @DATE 2020/3/31 15:11
 */
@Configuration
@AllArgsConstructor
public class FishBoneJwtTokenStoreConfiguration {

    private final KeyPair keyPair;

    /**
     * 使用jwtTokenStore存储token
     * @return
     */
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }


    /**
     * 生成JWT
     * @return
     */
    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setKeyPair(keyPair);
        return converter;
    }


    /**
     * 扩展JWT
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "jwtTokenEnhancer")
    public TokenEnhancer jwtTokenEnhancer() {
        return new FishBoneJwtTokenEnhancer();
    }
}
